package com.digital.smartgrow.service.expert.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.digital.smartgrow.common.util.BeanCopyUtil;
import com.digital.smartgrow.common.util.WebTool;
import com.digital.smartgrow.dto.expert.ExpertPageDTO;
import com.digital.smartgrow.entity.business.UpJob;
import com.digital.smartgrow.entity.business.UpJobLog;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.entity.expert.Expert;
import com.digital.smartgrow.dao.expert.ExpertMapper;
import com.digital.smartgrow.entity.export.ExportApprove;
import com.digital.smartgrow.service.business.UpBaseService;
import com.digital.smartgrow.service.business.UpJobLogService;
import com.digital.smartgrow.service.business.UpJobService;
import com.digital.smartgrow.service.datacenter.TeacherService;
import com.digital.smartgrow.service.expert.ExpertChooseService;
import com.digital.smartgrow.service.expert.ExpertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.smartgrow.service.export.ExportApproveService;
import com.digital.smartgrow.vo.expert.ExpertApproveVo;
import com.digital.smartgrow.vo.expert.ExpertPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 教师基本信息 服务实现类
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-16
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, Expert> implements ExpertService {

    @Autowired
    private UpJobLogService upJobLogService;
    @Autowired
    private UpBaseService upBaseService;
    @Autowired
    private UpJobService upJobService;
    @Autowired
    private ExpertChooseService expertChooseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ExportApproveService exportApproveService;
    @Override
    public Object getListByPage(ExpertPageDTO pageDTO) {
        Page<Expert> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<Expert> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getEmployeeId()), "employeeId", pageDTO.getEmployeeId());
        queryWrapper.eq(StringUtils.isNotBlank(pageDTO.getName()), "name", pageDTO.getName());
        queryWrapper.eq(StringUtils.isNotBlank(pageDTO.getStatus()+""), "status", pageDTO.getStatus());
        return this.page(page, queryWrapper);
    }
    @Override
    public Expert getInfoByUserName(String employeeId) {
        QueryWrapper<Expert> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Expert::getEmployeeId, employeeId);
        return this.getOne(queryWrapper);
    }

    @Override
    public Object approveList() {
        Teacher user = WebTool.getCurrentUser();
        String res=user.getIsExport()?"专家":"教师";
        /**
         * 专家审批设计(登录处已改造)：
         *         专家登录  可获取他可审批的列表，有审批权限
         *         教师登录 根据权限判定，可查看所有列表，但无审批权限
         *         教师是管理员 可查看所有列表，但无审批权限
         */
        //--------全部显示------
        //审核状态 待审核：0，审核中：1，通过：2，打回重新申报：-1,不通过：-2
        List<UpJobLog> list = upJobLogService.lambdaQuery().eq(UpJobLog::getStatus, 2).list();
        //list  进行专家筛选
        if(user.getIsExport()){
            list = exportFilter(user);
        }else{
            //默认和管理员显示的一样，后期权限在考虑
            list = upJobLogService.lambdaQuery().eq(UpJobLog::getStatus, 2).list();
        }
        if(list==null){
            return null;
        }
        //封装显示vo  ExpertApproveVo
        List<ExpertApproveVo> approveVoList=new ArrayList<>();
        for(UpJobLog l:list){
            Teacher infoByUserName = teacherService.getInfoByUserName(l.getUserCode());
            ExpertApproveVo vo = BeanCopyUtil.copyProperties(infoByUserName, ExpertApproveVo::new);
            vo.setUplogId(l.getId());
            UpJob byId = upJobService.getById(l.getUpJobCode());
            BeanCopyUtil.copyProperties(byId,vo);
            //审核数据填写
            ExportApprove one = exportApproveService.lambdaQuery().eq(ExportApprove::getUserCode, l.getUserCode())
                    .eq(ExportApprove::getExportId, user.getId()).one();
            if(one!=null){
                vo.setIsApprove(one.getIsApprive());
            }
            approveVoList.add(vo);
        }




        return approveVoList;
    }

    /**
     * 专家筛选，根据专家是否参与审批进行筛选
     * @param user
     */
    private List<UpJobLog>  exportFilter(Teacher user){
        List<ExportApprove> list = exportApproveService.lambdaQuery()
                .eq(ExportApprove::getIsProcess, 2)
                .eq(ExportApprove::getExportId, user.getId()).list();
        if(list!=null){
            List<UpJobLog> res=new ArrayList<>();
            for(ExportApprove e:list){
                Integer upJobLogId = e.getUpJobLogId();
                UpJobLog byId = upJobLogService.getById(upJobLogId);
                res.add(byId);
            }
            return res;
        }
        return null;
    }

    /**
     * 获取专家是否通过
     * @param upJobLog
     * @return
     */
    private Boolean isApprove(UpJobLog upJobLog){
        //todo
      return true;

    }
    @Override
    public Object getInfo(Integer id) {
        return this.getById(id);
    }
}
